package com.touchapk.javafx;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import com.touchapk.javafx.FileTreeCell.LuaStatus;

import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.control.TextArea;
import javafx.scene.control.TreeView;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;
import javafx.util.Callback;

/**
 * @description
 * @author yangkeyun
 * @email coyun@qq.com
 * @date 2016年8月16日
 * @version V1.0
 */
public class MainController {
	
	public static Map<String,LuaStatus> luaStatusMap=new HashMap<String,LuaStatus>();
	
	private TreeView<File> treeView;
	
	private TextArea console;
	
	private ResourceBundle i18nBundle;
	private Stage primaryStage;
	public MainController(Stage primaryStage,ResourceBundle i18nBundle){
		this.primaryStage=primaryStage;
		this.i18nBundle=i18nBundle;
	}
	
	/**
	 * @return the primaryStage
	 */
	public Stage getPrimaryStage() {
		return primaryStage;
	}

	/**
	 * @param primaryStage the primaryStage to set
	 */
	public void setPrimaryStage(Stage primaryStage) {
		this.primaryStage = primaryStage;
	}

	private FXMLDialog parent;
	
	private String path;
	
	
	/**
	 * @return the console
	 */
	public TextArea getConsole() {
		return console;
	}

	/**
	 * @param console the console to set
	 */
	public void setConsole(TextArea console) {
		this.console = console;
	}

	/**
	 * @return the treeView
	 */
	public TreeView<File> getTreeView() {
		return treeView;
	}

	/**
	 * @param treeView the treeView to set
	 */
	public void setTreeView(TreeView<File> treeView) {
		this.treeView = treeView;
	}

	/**
	 * @return the path
	 */
	public String getPath() {
		return path;
	}

	/**
	 * @param path the path to set
	 */
	public void setPath(String path) {
		this.path = path;
	}
	
	
	
	/**
	 * @return the i18nBundle
	 */
	public ResourceBundle getI18nBundle() {
		return i18nBundle;
	}
	
	private TouchapkWindowController touchapkController=new TouchapkWindowController(this);
	
	private SelectController selectController=new SelectController(this);
	
	private NewProjectController newProjectController=new NewProjectController(this);
	
	private NewController newController=new NewController(this);
	

	private DeleteController deleController=new DeleteController(this);
	
	private SelectDeviceController selectDeviceController=new SelectDeviceController(this);
	
	private SelectColorController selectColorController=new SelectColorController(this);
	
	
	public NewController getNewController(){
		return this.newController;
	}
	
	public DeleteController getDeleController(){
		return this.deleController;
	}
	
	public FXMLDialog getSelectDialog(){
		return new FXMLDialog("Touchapk",selectController,i18nBundle, SelectController.class.getResource("select.fxml"), primaryStage, StageStyle.DECORATED);
	}
	
	public FXMLDialog getTouchDialog(){
		if(parent==null){
			parent=new FXMLDialog("Touchapk", touchapkController,i18nBundle, TouchapkWindowController.class.getResource("touchapkWindow.fxml"), primaryStage, StageStyle.DECORATED);
			parent.getScene().getWindow().setOnCloseRequest(new EventHandler<WindowEvent>() {
				@Override
				public void handle(WindowEvent event) {
					Platform.runLater(new Runnable(){
						public void run(){
							System.exit(1);
						}
					});
				}
			});
		}
		return parent;
	}
	
	public FXMLDialog getNewProjectDialog(){
		
		return new FXMLDialog("New project", newProjectController,i18nBundle, NewController.class.getResource("newProject.fxml"),(parent==null? primaryStage:parent), StageStyle.DECORATED);
	}

	
	public FXMLDialog getDeleDialog(){
		
		return new FXMLDialog("Delete", deleController,i18nBundle, DeleteController.class.getResource("delete.fxml"),(parent==null? primaryStage:parent), StageStyle.DECORATED);
	}
	
	public FXMLDialog getNewDialog(){
		
		return new FXMLDialog("Create", newController,i18nBundle, Main.class.getResource("new.fxml"),(parent==null? primaryStage:parent), StageStyle.DECORATED);
	}
	
	public FXMLDialog getSelectDeviceDialog(){
		
		return new FXMLDialog("Devices", selectDeviceController,i18nBundle, Main.class.getResource("SelectDevice.fxml"),(parent==null? primaryStage:parent), StageStyle.DECORATED);
	}
	
	
	public FXMLDialog getSelectColorDialog(){
		
		return new FXMLDialog("Select color", selectColorController,i18nBundle, Main.class.getResource("selectColor.fxml"),  primaryStage , StageStyle.DECORATED);
		
	}
	
	
	private Callback callback;
	
	public void call(Callback callback){
		this.callback=callback;
	}
	
	public void runCall(Object is){
		this.callback.call(is);
	}
	
	
}
